package com.ply.controller;

import com.ply.entity.pojo.Manager;
import com.ply.entity.vo.Result;
import com.ply.service.ProjectService;
import com.ply.service.impl.ManageServiceImpl;
import com.ply.service.impl.UserServiceImpl;
import com.ply.utils.TokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @Author: hai jun
 * @Date: 2022/01/14/9:27
 */


@Slf4j
@RestController
@RequestMapping("/")
@CrossOrigin
public class ManageController {

    @Autowired
    ManageServiceImpl manageService;

    @Autowired
    ProjectService projectService;

    @Autowired
    UserServiceImpl userService;

//    @GetMapping("/admin/success")
//    public Result success(Integer cId){
//        if (cId > 0){
//            manageService.checkItem(cId);
//        }else{
//            return Result.fail("项目id不合法");
//        }
//        return Result.ok("审核通过");
//    }

    //    @GetMapping("/admin/defeated")
//    public Result defeated(Integer cId) {
//        System.out.println(cId);
//        return Result.fail("审核不通过");
//    }
    @PostMapping("/api/login.do")
    public Result<?> adminLogin(@RequestBody Manager manager,
                             HttpServletResponse response) {

        String name = manager.getName();
        String pwd = manager.getPasswd();


        //将Token 响应头 暴露出来
        response.setHeader("Access-Control-Expose-Headers", "Token");


        if (name == null || pwd == null || "".equals(name) || "".equals(pwd)) {
            return Result.fail("管理员name或密码为空");
        }


        //todo 还需要判断单点登录什么的
        Result<?> result = manageService.login(name, pwd);

        if (result.isTrue()) {
            Manager date = (Manager) result.getDate();
            log.info("用户:{}登陆成功", date.getName());

            String type = "管理员";
            //生成新的token
            String newToken = TokenUtils.toToken(date.getId(), type);

            System.out.println(newToken);

            //设置响应头
            response.setHeader("Token", newToken);

            return result;
        }

        return Result.fail("管理员登陆失败");
    }


//    @PostMapping("/admin/auditProject")
//    public Result<?> auditProject(@RequestBody ProjectAuditDto dto) {
//        Project project = projectService.selectProjectById(dto.getUserId(), dto.getProjectId()).getProject();
//        if (project.getStatus() != 2) {
//            return Result.fail("此项目不是待审核状态");
//        }
//        manageService.checkItem(20, project, dto.isPass(), dto.getCause());
//        return Result.ok();
//    }


    /**
     * 查询所有用户
     *
     * @return 用户的信息
     */
    @GetMapping("/admin/queryUserALl")
    public Result<?> queryUserALl() {
        return manageService.queryAllUser();
    }


    /**
     * 查询所有未审核的项目
     */
    @GetMapping("/admin/queryProjectAllByStatus")
    public Result<?> queryProjectAllByStatus(Integer status){
        return manageService.queryProjectAllByStatus(status);
    }


    /**
     * 更新项目的状态
     *
     * @param projectId 项目id
     * @param isTrue    是否通过
     * @param request   得到客服人员的id
     * @return 项目id，
     */
    @GetMapping("/admin/updateStatus")
    public Result<?> updateStatus(Integer projectId, int isTrue,
                               HttpServletRequest request) {
        //拿到 管理员id
        Integer adminId = (Integer) request.getAttribute("manageId");
        return manageService.checkVote(projectId, isTrue, adminId);
    }

    @GetMapping("/admin/queryLevelByUserId")
    public Result<?> queryLevelByUserId(int userId) {
        return manageService.queryLevelByUserId(userId);
    }

    @GetMapping("/admin/queryAllProject")
    public Result<?> queryAllProject(Integer pageIndex, Integer pageSum) {
        return manageService.queryAllProject(pageIndex, pageSum);
    }

    @GetMapping("/admin/queryVoteAll")
    //TODO 判断pageIndex和pageSum是否合法
    public Result<?> queryVoteAll(Integer pageIndex, Integer pageSum) {
        if (pageIndex <= 0 || pageSum <= 0) {
            return Result.fail("参数错误");
        }
        return manageService.queryAllProject(pageIndex, pageSum);
    }


    @GetMapping("/admin/queryAllByUserRecode")
    public Result<?> queryAllByUserRecode() {
        return manageService.queryAllByUserRecode();
    }


    @GetMapping("/admin/queryVoteRecord")
    public Result<?> queryVoteRecord(int projectId) {
        return manageService.queryVoteRecord(projectId);
    }

}
